package com.diandi.log;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
//import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

/**
 * 可通过外部配置修改参数支持
 *
 * @ConfigurationProperties("log.monitor")
 * Created by 袁恩光 on 2018/1/9.
 */
@Component
public class LogProperties implements ApplicationContextAware {


    //private MongoTemplate mongoTemplate;

    private ApplicationContext applicationContext;

    /**
     * 存储错误日志的库
     */
    private String logErrorCollection = "dd-error-log";

    /**
     * mongoTemplate bean 名称
     */
    private String mongoTemplateName = "mongoTemplate";


    /**
     * 程序名称 默认defaultName
     */
    private String applicationName = "defaultName";

    /**
     * 默认配置参数
     */
    private String[] params = new String[]{
            "serialNumber",
            "version",
            "accessPort",
            "systemType",
            "mobileType",
            "accessIp"};

//    public MongoTemplate getMongoTemplate() {
//        try {
//            Object mongoTemplate = applicationContext.getBean(this.mongoTemplateName);
//            if (null != mongoTemplate) {
//                this.mongoTemplate = (MongoTemplate) mongoTemplate;
//            }
//        } catch (BeansException e) {
//            return null;
//        }
//        return this.mongoTemplate;
//    }

/*    public void setMongoTemplate(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }*/

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        applicationContext = ac;
        String logErrorCollection = applicationContext.getEnvironment().getProperty("log.monitor.logErrorCollection");
        if (null != logErrorCollection) {
            this.logErrorCollection = logErrorCollection;
        }
        String mongoTemplateName = applicationContext.getEnvironment().getProperty("log.monitor.mongoTemplateName");
        if (null != mongoTemplateName) {
            this.mongoTemplateName = mongoTemplateName;
        }
        String params = applicationContext.getEnvironment().getProperty("log.monitor.params");
        if (null != params) {
            this.params = params.split(",");
        }
        String applicationName = applicationContext.getEnvironment().getProperty("spring.application.name");
        if (null != applicationName) {
            this.applicationName = applicationName;
        }
    }

    public String getLogErrorCollection() {
        return logErrorCollection;
    }

    public void setLogErrorCollection(String logErrorCollection) {
        this.logErrorCollection = logErrorCollection;
    }

    public String getMongoTemplateName() {
        return mongoTemplateName;
    }

    public void setMongoTemplateName(String mongoTemplateName) {
        this.mongoTemplateName = mongoTemplateName;
    }

    public String[] getParams() {
        return params;
    }

    public void setParams(String[] params) {
        this.params = params;
    }

    public String getApplicationName() {
        return applicationName;
    }

    public void setApplicationName(String applicationName) {
        this.applicationName = applicationName;
    }
}
